package spring.in.action.chapter_01.life;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class BeanPostPrcessorImpl implements BeanPostProcessor
{

    @Override
    public Object postProcessBeforeInitialization(Object pBean, String pBeanName)
            throws BeansException
    {
        if (pBean instanceof LifeBean)
        {
            LifeBean lBean = (LifeBean) pBean;
            int lIndex = lBean.getIndex();
            lBean.getOut().println(++lIndex + "、BeanPostProcessor.postProcessBeforeInitialization");
            lBean.setIndex(lIndex);
        }
        return pBean;
    }

    @Override
    public Object postProcessAfterInitialization(Object pBean, String pBeanName)
            throws BeansException
    {
        if (pBean instanceof LifeBean)
        {
            LifeBean lBean = (LifeBean) pBean;
            int lIndex = lBean.getIndex();
            lBean.getOut().println(++lIndex + "、BeanPostProcessor.postProcessAfterInitialization");
            lBean.setIndex(lIndex);
        }
        return pBean;
    }

}
